<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="上级部门ID，一级部门为0" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="上级部门ID，一级部门为0"></el-input>
    </el-form-item>
    <el-form-item label="唯一码" prop="uniqueId">
      <el-input v-model="dataForm.uniqueId" placeholder="唯一码"></el-input>
    </el-form-item>
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="部门名称"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="orderNum">
      <el-input v-model="dataForm.orderNum" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="是否删除  -1：已删除  0：正常" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="是否删除  -1：已删除  0：正常"></el-input>
    </el-form-item>
    <el-form-item label="类型：1.省级 2.市级 3.应用" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型：1.省级 2.市级 3.应用"></el-input>
    </el-form-item>
    <el-form-item label="状态：0 . 禁用 1.正常" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态：0 . 禁用 1.正常"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          deptId: 0,
          parentId: '',
          uniqueId: '',
          name: '',
          orderNum: '',
          delFlag: '',
          type: '',
          status: ''
        },
        dataRule: {
          parentId: [
            { required: true, message: '上级部门ID，一级部门为0不能为空', trigger: 'blur' }
          ],
          uniqueId: [
            { required: true, message: '唯一码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '是否删除  -1：已删除  0：正常不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型：1.省级 2.市级 3.应用不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态：0 . 禁用 1.正常不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.deptId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.deptId) {
            this.$http({
              url: this.$http.adornUrl(`/sys/sysdept/info/${this.dataForm.deptId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.parentId = data.sysDept.parentId
                this.dataForm.uniqueId = data.sysDept.uniqueId
                this.dataForm.name = data.sysDept.name
                this.dataForm.orderNum = data.sysDept.orderNum
                this.dataForm.delFlag = data.sysDept.delFlag
                this.dataForm.type = data.sysDept.type
                this.dataForm.status = data.sysDept.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/sysdept/${!this.dataForm.deptId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'deptId': this.dataForm.deptId || undefined,
                'parentId': this.dataForm.parentId,
                'uniqueId': this.dataForm.uniqueId,
                'name': this.dataForm.name,
                'orderNum': this.dataForm.orderNum,
                'delFlag': this.dataForm.delFlag,
                'type': this.dataForm.type,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
